#include "stdafx.h"
#include "ppStartup.h"
#include "TabSheet.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// TabSheet

TabSheet::TabSheet()
{
	m_nNumOfPages=0;
	m_nCurrentPage=0;
}

TabSheet::~TabSheet()
{
}


BEGIN_MESSAGE_MAP(TabSheet, CTabCtrl)
	//{{AFX_MSG_MAP(TabSheet)
	ON_WM_LBUTTONUP()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// TabSheet message handlers

void TabSheet::OnLButtonUp(UINT nFlags, CPoint point) 
{
	
	CTabCtrl::OnLButtonUp(nFlags, point);

	//
	if(m_nCurrentPage != GetCurFocus())
	{//
		m_pPages[m_nCurrentPage]->ShowWindow(SW_HIDE);
		m_nCurrentPage=GetCurFocus();
		m_pPages[m_nCurrentPage]->ShowWindow(SW_SHOW);
	}
}

void TabSheet::Show()
{
	int i=0;
	//create dialogs and insert pages
	for( i=0; i < m_nNumOfPages; i++ ){
		m_pPages[i]->Create( m_IDD[i], this );
		InsertItem( i, m_Title[i] );
	}
	//
	m_pPages[0]->ShowWindow(SW_SHOW);
	for( i=1; i < m_nNumOfPages; i++){
		m_pPages[i]->ShowWindow(SW_HIDE);
	}
	SetRect();
}

void TabSheet::SetRect()
{
	CRect tabRect, itemRect;
	int nX, nY, nXc, nYc;
	//get the size of Tab Control?
	GetClientRect(&tabRect);
	GetItemRect(0, &itemRect);
	//calculate the position of all child dialogs
	nX=itemRect.left;
	nY=itemRect.bottom+1;
	nXc=tabRect.right-itemRect.left-2;
	nYc=tabRect.bottom-nY-2;
	//modify the child dialogs position according the calculate result.
	m_pPages[0]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_SHOWWINDOW);
	for( int nCount=1; nCount < m_nNumOfPages; nCount++ ){
		m_pPages[nCount]->SetWindowPos(&wndTop, nX, nY, nXc, nYc, SWP_HIDEWINDOW);
	}
}

bool TabSheet::AddPage(LPCTSTR title, CDialog *pDialog,UINT ID)
{
	if( MAXPAGE == m_nNumOfPages )return false;
	//the number of total dialogs
	m_nNumOfPages++;
	//save the dialog ID and title.
	m_pPages[m_nNumOfPages-1] = pDialog;
	m_IDD[m_nNumOfPages-1] = ID;
	m_Title[m_nNumOfPages-1] = title;
	return true;
}
